package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.R;
import com.example.entity.Admin;
import com.example.entity.Admin2;
import com.example.service.Admin2Service;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("admin2s")
public class Admin2Controller {

    @Autowired
    private Admin2Service admin2Service;

    /**
     * 用户登录
     * @return
     */
    @PostMapping("/loginAdmin2")
    public R login(HttpServletRequest request, @RequestBody Admin2 admin2){

        LambdaQueryWrapper<Admin2> lqw =new LambdaQueryWrapper<>();

        lqw.eq(Strings.isNotEmpty(admin2.getJobNumber()),Admin2::getJobNumber,admin2.getJobNumber());
        lqw.eq(Strings.isNotEmpty(admin2.getPassword()),Admin2::getPassword,admin2.getPassword());

        Admin2 one = admin2Service.getOne(lqw);
        if (one ==null){
            return R.error("登录失败！");
        }else {
            return R.success("登录成功！",one);
        }
    }


    /**
     * 二级管理员分页查询
     * @return
     */
    @GetMapping("/getPageAdmin2/{current}/{size}")
    public R getPageAdmin2(@PathVariable int current,@PathVariable int size){

        IPage page =new Page(current,size);

        IPage page1 = admin2Service.page(page);

        return R.success("二级管理员分页查询！",page1);
    }

    /**
     * 添加二级管理员
     * @return
     */
    @PostMapping("/addAdmin2")
    public R addAdmin2(@RequestBody Admin2 admin2){

        LambdaQueryWrapper<Admin2> lqw =new LambdaQueryWrapper<>();
        lqw.eq(Strings.isNotEmpty(admin2.getJobNumber()),Admin2::getJobNumber,admin2.getJobNumber());
        //1.查询数据库此工号是否存在
        Admin2 one = admin2Service.getOne(lqw);

        //2.不存在在添加
        if (one==null){
            boolean save = admin2Service.save(admin2);
            if (save){
                return R.success("添加教职工成功！",null);
            }else {
                return R.error("添加教职工失败！");
            }
        }else {
            //3.存在
            return R.error("工号存在！添加失败！");
        }
    }

    /**
     * 通过id查询二级管理员
     * @return
     */
    @GetMapping("/getAdmin2ById")
    public R getAdmin2ById(@PathVariable int id){

        LambdaQueryWrapper<Admin2> lqw =new LambdaQueryWrapper<>();
        lqw.eq(Admin2::getId,id);
        Admin2 one = admin2Service.getOne(lqw);
        return R.success("通过id查询二级管理员",one);
    }

    /**
     * 通过id修改二级管理员
     * @return
     */
    @PutMapping("/updateAdmin2")
    public R updateAdmin2(@RequestBody Admin2 admin2){
        boolean b = admin2Service.updateById(admin2);
        if (b){
           return R.success("通过id修改二级管理员成功",admin2);
        }else {
           return R.error("通过id修改二级管理员失败！");
        }
    }

    /**
     * 通过id删除二级管理员
     * @return
     */
    @DeleteMapping("/deleteAdmin2/{id}")
    public R deleteAdmin2(@PathVariable int id){

        boolean b = admin2Service.removeById(id);
        if (b){
           return R.success("通过id删除二级管理员成功",id);
        }else {
           return R.error("通过id删除二级管理员失败！");
        }
    }

    /**
     * 根据id查询一级管理员信息
     * @return
     */
    @GetMapping("/getMyself/{id}")
    public R getMyself(@PathVariable int id){

        Admin2 one = admin2Service.getById(id);
        return R.success("根据id查询一级管理员信息(修改回显)",one);
    }



}

